package com.watson.tool.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * epp内部对线程池的封装，目前只是加了对线程池的监控日志
 *
 * @filename EppThreadPoolTaskExecutor.java
 *
 * @version 1.0
 *
 * @author cs
 *
 * @since 2019/3/17
 */
public class EppThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 打印线程池信息
	 */
	private void printThreadPoolInfo(){
		ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();

		logger.info("{},taskCount [{}], completedTaskCount [{}], activeCount [{}], queueSize [{}]",
				this.getThreadNamePrefix(),
				threadPoolExecutor.getTaskCount(),
				threadPoolExecutor.getCompletedTaskCount(),
				threadPoolExecutor.getActiveCount(),
				threadPoolExecutor.getQueue().size());
	}

	@Override
	public void execute(Runnable task) {
		printThreadPoolInfo();
		super.execute(task);
	}

	@Override
	public void execute(Runnable task, long startTimeout) {
		printThreadPoolInfo();
		super.execute(task, startTimeout);
	}

	@Override
	public Future<?> submit(Runnable task) {
		printThreadPoolInfo();
		return super.submit(task);
	}

	@Override
	public <T> Future<T> submit(Callable<T> task) {
		printThreadPoolInfo();
		return super.submit(task);
	}

	@Override
	public ListenableFuture<?> submitListenable(Runnable task) {
		printThreadPoolInfo();
		return super.submitListenable(task);
	}

	@Override
	public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
		printThreadPoolInfo();
		return super.submitListenable(task);
	}
}
